package cn.nju.zyy;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.nju.zyy.annotation.util.HibernateUtil;


public class Clear {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();

			session.createSQLQuery("drop table if exists COURSES").executeUpdate();
			
			// many 2 many
			session.createSQLQuery("drop table if exists STUDENT_COURSE").executeUpdate();
			// one 2 many
			session.createSQLQuery("drop table if exists STUDENT_PHONE").executeUpdate();
			
			session.createSQLQuery("drop table if exists STUDENT").executeUpdate();
			session.createSQLQuery("drop table if exists ADDRESS").executeUpdate();
			session.createSQLQuery("drop table if exists PHONE").executeUpdate();
			session.createSQLQuery("drop table if exists COURSE").executeUpdate();
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

}
